package com.example.hand.service.impl;


import com.example.hand.dataobject.CarDetail;
import com.example.hand.dataobject.CarInfo;
import com.example.hand.form.CarListForm;
import com.example.hand.repository.CarDetailRepository;
import com.example.hand.repository.CarInfoRepository;
import com.example.hand.service.CarInfoService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class CarInfoServiceImpl implements CarInfoService {

    @Autowired
    private CarInfoRepository carInfoRepository;

    @Autowired
    private CarDetailRepository carDetailRepository;

//    @Override
//    public Page<CarInfo> findAll(Pageable pageable) {
//        return carInfoRepository.findAllByOrderByModifyTimeDesc(pageable);
//    }

    @Override
    public Page<CarInfo> findListCarInfo(CarListForm params, Pageable pageable) {
        Page<CarInfo> list = carInfoRepository.findAll(new Specification<CarInfo>() {
            @Override
            public Predicate toPredicate(Root<CarInfo> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
                // build query condition

                List<Predicate> predicates = new ArrayList<Predicate>();
                if ((params.getCarTitleLike() != null) && !StringUtils.isEmpty((String) (params.getCarTitleLike()))) {
                    predicates.add(cb.like(root.get("carTitle").as(String.class), "%" + (String) params.getCarTitleLike() + "%"));
                }
                if (params.getBrandId() != null && !StringUtils.isEmpty((params.getBrandId()))) {
                    predicates.add(cb.equal(root.get("brandId").as(Integer.class), params.getBrandId()));
                }
                query.where(predicates.toArray(new Predicate[predicates.size()]));
                return null;
            }
        },pageable);

        return list;
    }

    @Override
    @Transactional
    public CarDetail add(CarDetail carDetail) {
        CarInfo carInfo = new CarInfo();
        BeanUtils.copyProperties(carDetail,carInfo);
        CarInfo carInfo1 = carInfoRepository.save(carInfo);
        carDetail.setCarId(carInfo1.getCarId());
        CarDetail objCarDetail = carDetailRepository.save(carDetail);
        return  objCarDetail;
    }

    @Override
    @Transactional
    public void delete(Integer Id) {
        carDetailRepository.deleteById(Id);
        carInfoRepository.deleteById(Id);
    }

    @Override
    public CarDetail findOne(Integer Id) {
        return carDetailRepository.findById(Id).get();
    }


    @Override
    @Transactional
    public CarDetail update(CarDetail carDetail) {
        CarDetail res = carDetailRepository.save(carDetail);
        CarInfo carInfo = new CarInfo();
        BeanUtils.copyProperties(carDetail,carInfo);
        return res;
    }
}
